/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.util.List;
import javax.swing.table.DefaultTableModel;
import pojo.NguoiDung;
import dao.NguoiDungDAO;
import java.util.Vector;
import javax.swing.JOptionPane;

/**
 *
 * @author BuiThiKieu
 */
public class TimKiemKhachHangGUI extends javax.swing.JPanel {

    /**
     * Creates new form TimKiemKhachHangGUI
     */
    private DefaultTableModel tableModel;
    private List<NguoiDung> nguoiDungList;
    
    public TimKiemKhachHangGUI(){
        initComponents();
        AutoLoad();
    }
    private void AutoLoad() {
        this.tableModel = new DefaultTableModel();
         /*
          * TẠO CÁC CỘT TRONG TABLE
          */
        this.tableModel.addColumn("ID Khách Hàng");
        this.tableModel.addColumn("Tên khách hàng");
        this.tableModel.addColumn("CMNN");
        this.tableModel.addColumn("Ngày sinh");
        this.tableModel.addColumn("Điện thoại");
        this.tableModel.addColumn("Địa chỉ");
        this.tbDanhSachKhachHang.setModel(tableModel);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtmaKhachHang = new javax.swing.JTextField();
        txtCMND = new javax.swing.JTextField();
        txttenKhachHang = new javax.swing.JTextField();
        btnTim = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbDanhSachKhachHang = new javax.swing.JTable();

        setLayout(null);

        jPanel1.setBackground(new java.awt.Color(255, 255, 204));

        jLabel1.setText("MÃ KHÁCH HÀNG");

        jLabel2.setText("CMND");

        jLabel3.setText("TÊN KHÁCH HÀNG");

        btnTim.setText("TÌM");
        btnTim.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTimActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(50, 50, 50)
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(37, 37, 37)
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(27, 27, 27)
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtmaKhachHang, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtCMND, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txttenKhachHang, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(136, 136, 136)
                        .addComponent(btnTim, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(60, 60, 60))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtmaKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(txtCMND, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(txttenKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32)
                .addComponent(btnTim, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        add(jPanel1);
        jPanel1.setBounds(160, 10, 400, 210);

        tbDanhSachKhachHang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tbDanhSachKhachHang);

        add(jScrollPane1);
        jScrollPane1.setBounds(20, 232, 670, 220);
    }// </editor-fold>//GEN-END:initComponents

    private void btnTimActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTimActionPerformed
        // TODO add your handling code here:
        AutoLoad();
        int maKhachHang = -1;
        if(txtmaKhachHang.getText().length() > 0){
            maKhachHang = Integer.parseInt(txtmaKhachHang.getText().toString());
        }
        //maKhachHang = Integer.parseInt(txtmaKhachHang.getText().toString());
        String cmnd = txtCMND.getText().toString();
        String tenKhachHang = txttenKhachHang.getText().toString();
        nguoiDungList = NguoiDungDAO.TimKiemKhachHang(maKhachHang, tenKhachHang, cmnd);
       // nguoiDungList = NguoiDungDAO.TimKhachHangTheoTen(tenKhachHang);
        if(nguoiDungList.size() == 0){
            JOptionPane.showMessageDialog(this, "Không tìm thấy");
            //txtmaKhachHang.setText("-1");
        }
        else
        {
            for(int i = 0; i < nguoiDungList.size(); i++){
                Vector vt = new Vector();
                vt.add(nguoiDungList.get(i).getId());
                vt.add(nguoiDungList.get(i).getTenNguoiDung());
                vt.add(nguoiDungList.get(i).getCmnd());
                vt.add(nguoiDungList.get(i).getNgaySinh());
                vt.add(nguoiDungList.get(i).getDienThoai());
                vt.add(nguoiDungList.get(i).getDiaChi());
                this.tableModel.addRow(vt);
                this.tbDanhSachKhachHang.setModel(tableModel);
                //txtmaKhachHang.setText("-1");
            }
        }
    }//GEN-LAST:event_btnTimActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnTim;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbDanhSachKhachHang;
    private javax.swing.JTextField txtCMND;
    private javax.swing.JTextField txtmaKhachHang;
    private javax.swing.JTextField txttenKhachHang;
    // End of variables declaration//GEN-END:variables
}
